# -*- coding:utf-8 -*-
'''
@author: xiaoming
@contact: lishihui0129@163.com
@time: 2017/6/29 16:24
@desc:
'''
import re
import pickle
from myUtil import flagSplitPattern
from demo.config import regionFile,shixianzhenFile
from ETL.myUtil import startEnd,isCompanyName,addressSuffix


pkl_file = open(regionFile, 'rb')
regionInfo = pickle.load(pkl_file)
pkl_file.close()

shixianzhen_file = open(shixianzhenFile, 'rb')
shixianzhen = pickle.load(shixianzhen_file)
shixianzhen_file.close()




# def isIncludeTarget(string,target):
#     '''
#     :param string:待判断的字符串
#     :param target: 需要识别字符串中是否包含目标对象，如：省、市、县区等
#     :return:true表示包含，false表示不包含
#     '''
#     if target in string:
#         return True
#     else:
#         return False

# def isAddressCombination(string,targetList):
#     '''
#     :param string:带识别对象
#     :param targetList: 省、市、县区待识别对象的列表
#     :return:true表示是地址组合、false表示不满足正常的地址组合
#     '''
#     for target in targetList:
#         if isAddress(string) and isIncludeTarget(string,target):
#             return True
#         else:
#             continue
#     return False


def addressExtract(string):
    '''
    :param string: 待提取对象
    :return:提取结果
    '''
    obj=startEnd()
    if len(string)>8 and (not isCompanyName(string,obj,regionInfo)):
        for shi in shixianzhen.keys():
            if shi in string:
                #获取该市对应的县（一个字典）
                xianzhen=shixianzhen[shi]
                for xian in xianzhen.keys():
                    if xian in string:
                        #获取镇（一个列表）
                        zhenList=xianzhen[xian]
                        if isIncludeAddressSuffix(addressSuffix,string):
                            return string.replace(u"地址:","").replace(u"地址：","").replace(u"地址","").replace(u"位于","").replace(u"总部座落于","")
                        else:
                            for zhen in zhenList:
                                if zhen in string:
                                    return shi+u'市'+xian+zhen
                            return shi+u'市'+xian

def isIncludeAddressSuffix(targetList,string):
    targetList=targetList.split("|")
    for each in targetList:
        # if each in string:
        if string.endswith(each):
            return True
        else:
            continue
    return False



# def getTargetIndex(string):
#     '''
#     :param string:
#     :return:返回目标对象开始位置的索引和结束位置的索引
#     '''
#
#     # string=string.replace(" ","").replace("　","")
#     string=string.lower()
#     if '联系地址' in string:
#         ind=string.index('联系地址')
#         end=ind+len('联系地址')
#     elif '所在地' in string:
#         ind=string.index('所在地')
#         end=ind+len('所在地')
#     elif '基地' in string:
#         ind=string.index('基地')
#         end=ind+len('基地')
#     elif '总部' in string:
#         ind=string.index('总部')+3
#         end=ind+len('总部')
#     elif '生产地' in string:
#         ind=string.index('生产地')
#         end=ind+len('生产地')
#     elif '厂址' in string:
#         ind=string.index('厂址')
#         end=ind+len('厂址')
#     elif '地址' in string:
#         ind=string.index('地址')
#         end=ind+len('地址')
#     elif 'address' in string:
#         ind=string.index('address')
#         end=ind+len('address')
#     elif 'add' in string:
#         ind=string.index('add')
#         end=ind+len('add')
#     else:
#         ind=0
#         end=0
#     return ind,end


# def prefixFlag(string):
#     '''
#     :param string: 待提取对象，并且string是未进行编码转换的原始字符串
#     :return:前缀信息
#     '''
#
#     ind=getTargetIndex(string)[0]
#     temp=string[:ind]
#     res=re.split(flagSplitPattern,temp)
#     jieguo=res[-1].strip()
#
#
#     if len(jieguo)>12:
#         return ""
#     else:
#         return res[-1].strip()


# def isFullAddress(string):
#     '''
#     :param string:待识别文本字符串
#     :return:true表示是一个完整地址；false表示不是一个完整地址
#     '''
#     if isAddress(string) and isIncludeTarget(string,'省') and isIncludeTarget(string,'市') and (isIncludeTarget(string,'区') or isIncludeTarget("县")):
#         return True
#     else:
#         return False

# def insertAddressTable(string):
#     end=getTargetIndex(string)[1]
#
#     temp=string[end:]
#     temp=re.split(" |:",temp)[1].strip().strip(";")
#     flag=prefixFlag(string)
#     connectDB()
#     sql="insert into address(address_value,prefix) values(%s,%s)"
#     param=(temp,flag)
#     insertSql(sql,param)
#     close()
